package com.hua.nailcampus.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.hua.nailcampus.acl.mapper.SchoolMapper;
import com.hua.nailcampus.acl.mapper.UserMapper;
import com.hua.nailcampus.acl.mapper.UserSchoolMapper;
import com.hua.nailcampus.acl.model.School;
import com.hua.nailcampus.acl.model.User;
import com.hua.nailcampus.acl.model.UserSchool;
import com.hua.nailcampus.acl.model.vo.UserSchoolVo;
import com.hua.nailcampus.acl.model.vo.UserVo;
import com.hua.nailcampus.acl.service.SchoolService;
import com.hua.nailcampus.constant.RedisConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author:chenjianhua
 * @Date:2023/11/15 18:05
 * @Version 17
 */
@Service
public class SchoolServiceImpl extends ServiceImpl<SchoolMapper, School> implements SchoolService {

    @Autowired
    private SchoolMapper schoolMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserSchoolMapper userSchoolMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<School> getSchoolList() {
        List<School> schoolList = schoolMapper.selectList(null);
        redisTemplate.opsForValue().set(RedisConst.SCHOOL_KEY,schoolList,RedisConst.SCHOOL_TIMEOUT, TimeUnit.MINUTES);
        return schoolList;
    }

    @Override
    public UserSchoolVo getSchool(Integer userId) {
        UserSchool userSchool = userSchoolMapper.selectOne(new LambdaQueryWrapper<UserSchool>()
                .eq(UserSchool::getUserId, userId)
                .eq(UserSchool::getStatus, true));
//        判断是否认证,若没有进行学校认证，则直接返回null
        if(userSchool==null){
            return null;
        }
        UserSchoolVo userSchoolVo = new UserSchoolVo(userSchool);
        userSchoolVo.setSchoolName(schoolMapper.selectById(userSchool.getSchoolId()).getName());
        return userSchoolVo;
    }

    @Override
    public void addUserSchool(UserSchool userSchool) {
        userSchoolMapper.insert(userSchool);
    }

    @Override
    public UserSchool isApply(Integer userId) {
        UserSchool userSchool = userSchoolMapper.selectOne(new LambdaQueryWrapper<UserSchool>()
                .eq(UserSchool::getUserId, userId));
        return userSchool;
    }

    @Override
    public IPage<UserSchoolVo> getUserSchoolList(Integer current,Integer limit,UserSchool userSchool) {
        LambdaQueryWrapper<UserSchool> wrapper = new LambdaQueryWrapper<>();
        if(userSchool.getSchoolId()!=null){
            wrapper.eq(UserSchool::getSchoolId,userSchool.getSchoolId());
        }
        if(userSchool.getStatus()!=null){
            wrapper.eq(UserSchool::getStatus,userSchool.getStatus());
        }
        IPage<UserSchoolVo> userSchoolVoPage = new Page<>(current, limit);
        List<UserSchool> userSchoolList = userSchoolMapper.selectList(wrapper);
        List<UserSchoolVo> userSchoolVoList = new ArrayList<>();
        for(UserSchool userSchool1:userSchoolList){
            UserSchoolVo userSchoolVo = new UserSchoolVo(userSchool1);
            User user = userMapper.selectById(userSchool1.getUserId());
            userSchoolVo.setUserName(user.getUserName());
            School school = schoolMapper.selectById(userSchool1.getSchoolId());
            userSchoolVo.setSchoolName(school.getName());
            userSchoolVoList.add(userSchoolVo);
        }
        userSchoolVoPage.setRecords(userSchoolVoList);
        return userSchoolVoPage;
    }

    @Override
    public void passAllUserSchool() {
        // 获取所有待审核的记录
        LambdaQueryWrapper<UserSchool> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserSchool::getStatus, false);
        List<UserSchool> userSchoolList = userSchoolMapper.selectList(wrapper);
        // 批量更新状态为true（审核通过）
        for (UserSchool userSchool : userSchoolList) {
            userSchool.setStatus(true);
            userSchoolMapper.updateById(userSchool);
        }
    }
}

